<script setup lang="ts">
import { ref } from "vue";
import tree from "./tree.vue";
import { useUser } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { ArrowDown } from '@element-plus/icons-vue'

import Refresh from "~icons/ep/refresh";

defineOptions({
  name: "SystemUser"
});

const treeRef = ref();
const formRef = ref();
const tableRef = ref();

const {
  permissions,

  form,
  loading,
  columns,
  dataList,

  deptData,
  deptLoading,

  pagination,
  deviceDetection,
  onSearch,
  resetForm,
  openDialog,
  onTreeSelect,
  handleCommand,
  handleSizeChange,
  handleCurrentChange,
} = useUser(tableRef, treeRef);
</script>

<template>
  <div :class="['flex', 'justify-between', deviceDetection() && 'flex-wrap']">
    <tree
      ref="treeRef"
      :class="['mr-2', deviceDetection() ? 'w-full' : 'min-w-[200px]']"
      :treeData="deptData"
      :treeLoading="deptLoading"
      @tree-select="onTreeSelect"
    />
    <div
      :class="[deviceDetection() ? ['w-full', 'mt-2'] : 'w-[calc(100%-200px)]']"
    >
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="" prop="key">
          <el-input
            v-model="form.key"
            placeholder="成员用户名/昵称/手机号"
            clearable
            class="w-[180px]!"
          />
        </el-form-item>
        <el-form-item prop="status">
          <el-select
            v-model="form.status"
            placeholder="启用状态"
            clearable
            class="w-[120px]!"
          >
            <el-option label="启用" value="1" />
            <el-option label="停用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri/search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>

        <el-form-item v-if="permissions.is_member_add">
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:add-large-line')"
            @click="openDialog()"
          >
            新增
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar
        title=""
        :columns="columns"
        @refresh="onSearch"
        :key="columns.length"
      >
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-dropdown @command="handleCommand(row, $event)">
                <el-button type="primary" size="small">
                  操作<el-icon class="el-icon--right"><ArrowDown /></el-icon>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item command="edit" v-if="permissions.is_member_edit">编辑</el-dropdown-item>
                    <el-dropdown-item command="reset" v-if="permissions.is_member_resetPassword">重置密码</el-dropdown-item>
                    <el-dropdown-item divided command="delete" v-if="permissions.is_member_del">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-form--inline .el-form-item) {
  margin-right: 12px;
}

:deep(.el-button:focus-visible) {
  outline: none;
}

.main-content {
  margin: 12px 12px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
